const tokenKey = "APP-TOKEN"
import { LoginApi } from '@/api/user/LoginApi'
import { Common } from '@/utils/Common'

export const Auth = {

    getToken: () => {
        return uni.getStorageSync(tokenKey)
    },

    setToken: (token) => {
        uni.setStorageSync(tokenKey, token)
    },

    removeToken: () => {
        uni.removeStorageSync(tokenKey)
    },

    isLogin: () => {
        return !!uni.getStorageSync(tokenKey);
    },
	setWxOpenId:(openId)=>{
		uni.setStorageSync('wxOpenId', openId)
	},
	getWxOpenId:()=>{
		uni.getStorageSync('wxOpenId')
	},
    toLogin: () => {
		// #ifdef H5
		if(Common.isWechat()){
			uni.navigateTo({
			    url: "/pages/user/login/BindMobile"
			})
		}else{
			uni.navigateTo({
			    url: "/pages/user/login/PwdLogin"
			})
		}
		// #endif
		
		// #ifdef MP-WEIXIN
		uni.showLoading({
		    "title": "登录中...",
		    "mask": true
		});
		uni.login({
		    provider: 'weixin',
		    success: (res) => {
		        // 下边是可以登录
		        LoginApi.wxLogin(res.code).then((response) => {
		            if (response.code === -3) {
						Auth.setWxOpenId(response.data)
						uni.navigateTo({
						    url: "/pages/ucenter/login/BindMobile"
						})
		            } else {
		                this.Auth.setToken(response)
		                this.$store.dispatch('user/getInfo').then(() => {
		                    
		                })
		            }
		            uni.hideLoading()
		        })
		    }
		})
		// #endif
		// #ifdef APP-PLUS
		uni.navigateTo({
		    url: "/pages/user/login/PwdLogin"
		})
		// #endif
    }
}
